package com.order.dao;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;

@Repository("jdbcDao")
public class jdbcDao {
	public static final String DRIVER = "com.mysql.jdbc.Driver";
	public static final String DBURL = "jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf-8";
	public static final String DBUSER = "root";
	public static final String DBPASS = "root";

	Connection con = null;
	PreparedStatement pStat = null;
	ResultSet rs = null;

	public Connection getConnection() {
		Connection con = null;
		try {
			Class.forName(DRIVER);
			con = (Connection) DriverManager.getConnection(DBURL, DBUSER, DBPASS);
		} catch (ClassNotFoundException e) {
			System.out.println("数据库驱动未找到!");
			e.printStackTrace();
		} catch (SQLException e) {
			System.out.println("数据库连接失败!");
			e.printStackTrace();
		}
		return con;
	}
	
	public boolean find(Integer studentId , String nDate) {
		System.out.println("jdbc");
		con = getConnection();
		try {
			pStat = con.prepareStatement("select nextDate from student where sId=? ");
			pStat.setInt(1, studentId);
			//pStat.setString(2, password);
			rs = pStat.executeQuery();
			
			System.out.println("rs:"+rs);
			System.out.println("==============================");
			
			System.out.print("rs_date:");
			System.out.println(rs.next() == true?rs.getString("nextDate"):"错误！");
			
			System.out.println("==============================");
			
			//rs.next()
			if (rs.getString("nextDate").equals(nDate))
				return true;
			else
				return false;
		} catch (Exception e) {
			return false;
		}
	}
	
	

	/**
	 * 释放资源
	 */
	public void close() {
		try {
			if (rs != null) {
				rs.close();
			}
			if (pStat != null) {
				pStat.close();
			}
			if (con != null) {
				con.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
